package com.it.controller;

import com.it.pojo.CRUDresult;
import com.it.pojo.CourseOrder;
import com.it.pojo.PageModel;
import com.it.service.IcourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hp
 * 跳转页面
 */
@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {
    @Autowired
    IcourseOrderService icourseOrderService;
    @RequestMapping("list")
    public String list(){
        return "courseorder/list";
    }
    @RequestMapping("add")
    public String add(){
        return "courseorder/add";
    }
    @RequestMapping("edit")
    public String edit(Model model,String order_id){
        CourseOrder order =icourseOrderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        System.out.println("order id"+order_id);
        return "courseorder/edit";
    }
    @RequestMapping("detail")
    //把从数据库取得值放入模型 model
    public String detail(Model model,String order_id){
        CourseOrder order =icourseOrderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "courseorder/detail";
    }
    @RequestMapping("detele")
    @ResponseBody
    //把从数据库取得值放入模型 model
    public CRUDresult detele(String order_id){
         CRUDresult result =new CRUDresult();
          icourseOrderService.deleteByID(order_id);
        return result;
    }
    //放入entity参数
    @RequestMapping("save")
    @ResponseBody
    public CRUDresult save(CourseOrder order){
        CRUDresult result =new CRUDresult();
        //如果有值就是修改
        if(order.getOrder_id()==null|| order.getOrder_id().length()==0){
            icourseOrderService.save(order);
        }else {
            icourseOrderService.upDate(order);
        }
        System.out.println(order);


        return result ;
    }


    /**
     * 因为PageModel 用的泛型 所以通用 我们可以写假数据
     * @return
     */
    @RequestMapping("listjson")
    @ResponseBody
    public PageModel<CourseOrder> listJson(CourseOrder condition, int page,int limit){
        System.out.println(condition);
        PageModel<CourseOrder> result = icourseOrderService.pageModel(condition,page,limit);
        return result;
    }
}
